package com.zhengqian.system.service.impl;


import com.zhengqian.commons.SystemUtils;
import com.zhengqian.system.entity.UserEntity;
import com.zhengqian.system.info.UserInfo;
import com.zhengqian.system.repository.UserRepository;
import com.zhengqian.system.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {

    final int ZERO = 0;
    final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);


    @Autowired
    UserRepository userRepository;

    /**
     * 添加用户
     * @param userEntity
     * @return true:保存成功
     */
    public boolean addUser(UserEntity userEntity){
        UserInfo user = null;
        logger.info("system user service addUser start : " + userEntity);

        logger.info("system user service addUser userRepository save start ");
        try{
            user = userRepository.save(zhuanHuan(userEntity));
            logger.info("system user service addUser userRepository save end " );
            logger.info("user:" + user);
        }catch (Exception exception){
            logger.error("system user service addUser fail" + exception);
            return false;
        }

        if(!SystemUtils.isNull(user) && user.getUid() != ZERO){
            logger.info("system user service addUser success");
            return true;
        }
        logger.error("system user service addUser fail");
        return false;
    }

    /**
     * 删除用户
     * @param uid 需要删除的id
     * @return true:删除成功
     */
    @Override
    public boolean delUser(String uid) {

        //删除一条删除多条的业务逻辑
        String[] ids = uid.split(",");

        if(SystemUtils.isNull(ids) || ids.length == 0){
            return false;
        }

        //删除一条
        if(ids.length == 1){
            //先查询后更新;
            try{
                Long id = Long.parseLong(ids[0]);
                UserInfo userInfo = userRepository.findById(id).get();
                userInfo.setUstatus(1);
                userRepository.save(userInfo);
                return true;
            }catch (Exception exception){
                return false;
            }

        }else{ //删除多条

            try{
                Set<Long> sets = new HashSet<>();
                for(String id : ids){
                    sets.add(Long.parseLong(id));
                }
                userRepository.updates(sets);
                return true;
            }catch (Exception exception){
                return false;
            }
        }
    }

    /**
     * 修改用户
     * @param userEntity 需要修改的信息
     * @return true:修改成功
     */
    @Override
    public boolean updUser(UserEntity userEntity) {

        //1: 去数据库里面查询有没有这个记录
        UserInfo userInfo = userRepository.findById(userEntity.getUid()).get();
        //2:根据你前端来的信息去进行修改

        //代表这个信息不是空;需要修改
        if(!SystemUtils.isNullOrEmpty(userEntity.getUname())){
            userInfo.setUname(userEntity.getUname());
        }

        //手机修改
        if(!SystemUtils.isNullOrEmpty(userEntity.getUphone())){
            userInfo.setUphone(userEntity.getUphone());
        }

        if(!SystemUtils.isNullOrEmpty(userEntity.getUmail())){
            userInfo.setUmail(userEntity.getUmail());
        }

        UserInfo save = userRepository.save(userInfo);

        return true;
    }

    /**
     * 将UserEntity 转换成  UserInfo
     * @param userEntity
     * @return UserInfo
     */
    private UserInfo zhuanHuan(UserEntity userEntity){

        logger.info("system user service zhuanHuan start : " + userEntity);

        UserInfo userInfo = new UserInfo();

        userInfo.setT1(userEntity.getT1());
        userInfo.setUaccount(userEntity.getUaccount());

        //注意一下
        userInfo.setUdesc(userEntity.getDesc());

        userInfo.setUid(userEntity.getUid());
        userInfo.setUmail(userEntity.getUmail());
        userInfo.setUname(userEntity.getUname());
        userInfo.setUpass(userEntity.getUpass());
        userInfo.setUphone(userEntity.getUphone());

        logger.info("system user service zhuanHuan end userInfo : " + userInfo);

        return userInfo;
    }

}
